<?php

namespace App\Models;

use App\Libraries\Common\CommonFun;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use HasFactory;

    protected $table = 'article';


    //查询数据列表
    public static function lists($page = 1, $limit = 20, $params = []){
        $ary_where = array();
        $ary_where[]=['is_del','=',0];
        $title=data_get($params,'title',false);

        if($title){
            $ary_where[] = ['title', 'like', '%'.$title.'%'];
        }

        $query = self::where($ary_where);
        $count = $query->count();
        $data = $query->offset(CommonFun::getOffset($page, $limit))->limit($limit)->orderBy('id', 'desc')->get()->map(function (Article $rebate){
            $rebate = $rebate->attributesToArray();
            $rebate['week']='';
            $rebate['month']='';
            $rebate['is_status'] = $rebate['status']==1 ?'已发送' : '未发送';
            switch ($rebate['type']){
                case 'at_current':
                    $rebate['type_article'] ='立即发送';
                    break;
                case 'at_date':
                    $rebate['type_article'] ='定时发送';
                    break;
                case 'every_day':
                    $rebate['type_article'] ='每天';
                    break;
                case 'every_week':
                    $rebate['type_article'] ='每周';
                    break;
                case 'every_month':
                    $rebate['type_article'] ='每月';
                    break;
                default:
                    $rebate['type_article'] ='';
            }
            if($rebate['type']=="every_week"){

                $rebate['week']=substr($rebate['time'],0,strpos($rebate['time'],":"));
                $rebate['time']=substr($rebate['time'],stripos($rebate['time'],":")+1);
            }
            if($rebate['type']=="every_month"){
                $rebate['month']=substr($rebate['time'],0,strpos($rebate['time'],":"));
                $rebate['time']=substr($rebate['time'],stripos($rebate['time'],":")+1);
            }
            return $rebate;
        })->toArray();

        return $data ? compact('count', 'data') : false;
    }

    //对消息进行添加操作
    public static function createApi($data){

        if(! $id=data_get($data,'id',false)){
            $model=new self();
            //生成唯一编号
            $url=RsConfig::query()->where(['id'=>1])->first();
            $sn=CommonFun::createTradeId('artilce');
            $model->title=$data['title'];
            if($data['content']){
                $model->content=$data['content'];
            }

            $model->status=$data['status'];
            $model->type=$data['type'];
            $model->time=$data['time'];
            $model->description=$data['description'];
            $model->url=$url->attr_value."?article_sn=".$sn;
            if($data['picurl']){
                $model->picurl=$data['picurl'];

            }
            $model->article_sn=$sn;
            $model->category=$data['category'];
            $model->created_at=date('Y-m-d H:i:s');
        }else{
            $model = self::where(['id'=>$id])->first();
            $model->title=$data['title'];
            if($data['content']){
                $model->content=$data['content'];
            }
            $model->status=$data['status'];
            $model->type=$data['type'];
            $model->time=$data['time'];
            $model->description=$data['description'];
            if($data['picurl']){
                $model->picurl=$data['picurl'];

            }
            $model->category=$data['category'];
            $model->updated_at=date('Y-m-d H:i:s');
        }

        return $model->save() ?true:false;
    }

    /**
     * 格式化时间
     * */
    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function material(){
        return $this->hasOne(RsMaterial::class,'id','rs_material_id');
    }
}
